package com.hx.oracle;

import com.hx.oracle.entity.Regions;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class Exercise {
    public static void main(String[] args) {
        try {
            //加载驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            System.out.println("驱动加载成功");
            //加载连接
            String ulr="jdbc:oracle:thin:@localhost:1521:xe";
            String user="hr";
            String password="123";
            Connection conn= DriverManager.getConnection(ulr,user,password);
            System.out.println("连接成功");
            Statement statement=conn.createStatement();
            String sql="select * from regions";
            ResultSet rs=statement.executeQuery(sql);
            List<Regions> regions=new ArrayList<>();
            while (rs.next()){
                Regions r=new Regions();
                r.setRegionId(rs.getInt("region_id"));
                r.setRegionName(rs.getString("region_name"));
                regions.add(r);
                //regions.add(new  Regions(rs.getInt(1),rs.getString(2)));
            }
            rs.close();
            for(Regions r:regions){
                System.out.println(r);
            }

//          String sql="select * from employees";
//          ResultSet rs=statement.executeQuery(sql);
            /*rs.next();
            System.out.println(rs.getString("first_name"));
            System.out.println(rs.getString(2)+"\t"+rs.getString(3));//显示全名*/

            //查询语句的拼接
//            String regionName="中国";
//            Integer regionId=8;
//            String sql="insert into regions(region_id,region_name) values("+regionId+",'"+regionName+"')";
//            System.out.println(sql);
//            int a=statement.executeUpdate(sql);
//            System.out.println(a);


            //通过预编译来实现sql语句的灵活配置

//            String sql="insert into regions(region_id,region_name) values(?,?)";
//            PreparedStatement preparedStatement=conn.prepareStatement(sql);
//            String regionName="云南";
//            Integer region_id=6;
//            preparedStatement.setInt(1,region_id);
//            preparedStatement.setString(2,regionName);
//            preparedStatement.executeUpdate();


//            while (rs.next()){
//                System.out.println(rs.getString(2)+"\t"+rs.getString(3));
//            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
